import type { ElMessageBoxOptions } from "element-plus";
import { SvgIcon } from "@/components/Icon";
import { ElMessageBox, ElNotification, ElMessage } from "element-plus";
import type { DiyNotificationArgsProps } from "#/notification";

function getNotifyIcon(iconType: string) {
  if (iconType === "warning") {
    return <SvgIcon size="20" name="warning" />;
  } else if (iconType === "success") {
    return <SvgIcon size="20" name="success" />;
  } else if (iconType === "error") {
    return <SvgIcon size="20" name="errorNotify" />;
  } else {
    return <SvgIcon size="20" name="warning" />;
  }
}

/**
 * @description: Create confirmation box
 */
function createConfirm(options: ElMessageBoxOptions) {
  const opt: ElMessageBoxOptions = Object.assign(
    {
      title: "温馨提示",
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      lockScroll: true,
      center: false
    },
    { ...options }
  );
  return ElMessageBox.confirm(opt.message, opt.title || "温馨提示", opt);
}

function createNotification(options: DiyNotificationArgsProps) {
  const iconType = options.type || "warning";
  Reflect.deleteProperty(options, "icon");
  const opt = {
    icon: getNotifyIcon(iconType),
    type: options.type || "warning",
    class: "notify-title-line-height",
    title: options.title || "温馨提示",
    ...options
  };
  return ElNotification(opt);
}

const getBaseOptions = () => {
  return {
    duration: 3000,
    showClose: true,
    closeOnPressEscape: false,
    closeOnClickModal: false,
    showCancelButton: false,
  };
};

function createModalOptions(options, iconType: string): ElMessageBoxOptions {
  return {
    ...getBaseOptions(),
    ...options,
    type: iconType,
    confirmButtonText: "确认",
    icon: getNotifyIcon(iconType)
  };
}

function createSuccessModal(options: ElMessageBoxOptions) {
  return ElMessageBox(createModalOptions(options, "success"));
}

function createErrorModal(options: ElMessageBoxOptions) {
  return ElMessageBox(createModalOptions(options, "error"));
}

function createInfoModal(options: ElMessageBoxOptions) {
  return ElMessageBox(createModalOptions(options, "info"));
}

function createWarningModal(options: ElMessageBoxOptions) {
  return ElMessageBox(createModalOptions(options, "warning"));
}

/**
 * @description: message
 */
export function useMessage() {
  return {
    createMessage: ElMessage,
    createNotification: createNotification,
    createConfirm: createConfirm,
    createSuccessModal,
    createErrorModal,
    createInfoModal,
    createWarningModal
  };
}
